import { TRequest } from "@/api/Modules/HttpRequest"
import { ref } from "vue"

interface IUseNoPageListConfig<T> {
    apiFunction: (params?: any) => TRequest<T[]>
    dataFields?: string
}
/**
 * 无分页列表请求
 * @param originConfig 
 * @returns 
 */
export const useListNopage = <T>(originConfig: IUseNoPageListConfig<T>) => {
    const config: IUseNoPageListConfig<T> = originConfig
    const currentItem = ref<T>()
    interface IState {
        params: {
            [x: string]: number;
        };
    }
    const dataSource = ref<T[]>([])
    /**
     * 加载数据
     * @param otherParams 
     * @returns 
     */
    const loadData = async (otherParams?: any) => {
        let result = await config.apiFunction({ ...otherParams });
        dataSource.value = result[config.dataFields ?? "data"] as any
    }
    const setCurrentItem = async (item: T) => {
        currentItem.value = item
    }
    return {
        loadData,
        dataSource,
        setCurrentItem,
        currentItem
    }
}

